package com.data.center.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.data.center.entity.foreignDemo.Children1;
import com.data.center.mapper.Children1Mapper;
import com.data.center.service.Children1Service;
import com.data.center.util.ForeignManage.ForeignDataItem;
import com.data.center.util.ForeignManage.ForeignManageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

@Service
@Slf4j
public class Children1ServiceImpl implements Children1Service {
    @Autowired
    Children1Mapper children1Mapper;
    @Autowired
    ForeignManageUtils foreignManageUtils;
    @Autowired
    ApplicationContext applicationContext;

    @Override
    public List<Children1> searchByForeignKey(String id) {
        return children1Mapper.searchByForeignKey(id);
    }

    @Override
    public int deleteByForeignKey(String id) {
        return children1Mapper.deleteByForeignKey(id);
    }

    /**
     * @description:fatherId外键上的合并方法，将fatherId为deletedId的数据的fatherId修改为id
     * @param id
     * @param deletedId
     * @return
     */
    @Override
    public int mergeByForeignKey(String id, String deletedId) {
        log.info("mergeByForeignKey参数" + id + "  " + deletedId );
        List<Children1> deletedChildren1 = children1Mapper.searchByForeignKey(deletedId);
        for (Children1 mergeItem : deletedChildren1) {
            mergeItem.setFatherId(id);
            children1Mapper.update(mergeItem);
        }
        return 1;
    }

    /**
     * 合并两个children1的方法，输入两个将要合并的主键
     * @param id
     * @param deletedId
     * @return
     */
    @Override
    public int merge(String id, String deletedId) {

        // 需要首先检查即将被删除的deletedId是否被其他表引用，如果被引用则调用其他表的根据其他表的依赖键的对应合并方法进行合并。
        Children1 savedChildren1 = children1Mapper.searchById(id); // 保留下来的实体
        Children1 deletedChildren1 = children1Mapper.searchById(deletedId); // 需要对将被删除的实体对象进行依赖检查

        for (ForeignDataItem item : foreignManageUtils.getForeignDataItem(Children1.class.getName())) {
            try {
                // 针对一个依赖关系，首先查询其他表中依赖本表的键对于即将被删除的实体的对应键的属性是否有数据
                Object service = applicationContext.getBean((Class)item.serviceClass);
                List list = (List) item.searchByForeignKeyMethod.invoke(service, BeanUtil.getFieldValue(deletedChildren1, item.foreignKey));
                if (list != null && list.size() > 0) { // 如果有，需要调用对应键的合并方法
                    item.mergeByForeignKeyMethod.invoke(
                            service, BeanUtil.getFieldValue(savedChildren1, item.foreignKey),
                            BeanUtil.getFieldValue(deletedChildren1, item.foreignKey)
                    );
                }
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            }
        }

        children1Mapper.deleteByForeignKey(deletedId);

        return 1;
    }
}
